package kiwi.assist.dev.guieditor.command {
	import fl.core.UIComponent;

	import ghostcat.util.ReflectUtil;

	import kiwi.assist.dev.guieditor.event.GUIEditorEvent;
	import kiwi.assist.dev.guieditor.model.ContainerConfig;
	import kiwi.assist.dev.guieditor.model.GUIEditorConfig;
	import kiwi.assist.dev.guieditor.model.UiDetailVO;
	import kiwi.assist.dev.reflect.FlashUiComponentReflectFactoryImpl;
	import kiwi.assist.dev.reflect.IReflectFactory;

	import org.aswing.AsWingUtils;
	import org.aswing.ButtonGroup;
	import org.aswing.JButton;
	import org.aswing.JFrame;
	import org.aswing.JPanel;
	import org.aswing.JRadioButton;
	import org.aswing.SoftBoxLayout;
	import org.aswing.geom.IntPoint;
	import org.robotlegs.mvcs.Command;

	import flash.display.DisplayObject;
	import flash.events.MouseEvent;

	/**
	 * @author zhangming.luo
	 */
	public class ShowContainerSelectDialogCommand extends Command {
		[Inject]
		public var config : GUIEditorConfig;
		[Inject]
		public var detail : UiDetailVO;
		[Inject]
		public var event : GUIEditorEvent;
		private var frame : JFrame;
		private var content : JPanel;
		private var group : ButtonGroup;
		private var listPanel : JPanel;
		private var okButton : JButton;

		override public function execute() : void {
			super.execute();
			if (event.data) {
				select(event.data);
				return;
			}
			frame = new JFrame(contextView.stage, "选择容器", true);
			// 显示容器列表
			frame.setContentPane(content = new JPanel());
			content.setLayout(new SoftBoxLayout(SoftBoxLayout.Y_AXIS));
			listPanel = new JPanel();
			group = new ButtonGroup();
			for each (var container:ContainerConfig in config.containers) {
				var button : JRadioButton = new JRadioButton(container.name);
				if (container == detail.container) {
					button.setSelected(true);
				}
				listPanel.append(button);
				group.append(button);
			}
			content.append(listPanel);
			content.append(okButton = new JButton("确定"));
			okButton.addEventListener(MouseEvent.CLICK, function(event : MouseEvent) : void {
				frame.closeReleased();
				select(config.containers[group.getSelectedIndex()]);
			});
			frame.pack();
			frame.setResizable(false);
			var location : IntPoint = AsWingUtils.getScreenCenterPosition();
			location.x = Math.round(location.x - frame.getWidth() / 2);
			location.y = Math.round(location.y - frame.getHeight() / 2);
			frame.setLocation(location);
			frame.show();
		}

		private function select(container : ContainerConfig) : void {
			detail.container = container;
			var factory : IReflectFactory;
			if (container.factoryClz) {
				// 有工厂的
				factory = injector.instantiate(ReflectUtil.getClass(container.factoryClz));
			} else {
				// 走默认工厂
				factory = injector.instantiate(FlashUiComponentReflectFactoryImpl);
			}
			var dos : DisplayObject = DisplayObject(factory.newInstance(container.defaultConfig));
			container.instanceMap[dos] = true;
			if (detail.containerImpl) {
				dos.width = detail.containerImpl.width;
				dos.height = detail.containerImpl.height;
			}
			detail.containerImpl = UIComponent(dos);
			dispatch(new GUIEditorEvent(GUIEditorEvent.CONTAINER_CHANGE, dos));
		}
	}
}
